package com.sqlparser.core.lexer.impl;

import com.sqlparser.core.lexer.Lexer;
import com.sqlparser.core.lexer.Token;
import com.sqlparser.core.lexer.TokenType;
import com.sqlparser.common.exception.SQLParserException;

/**
 * 词法分析器实现类
 */
public class SimpleLexer implements Lexer {
    private final String input;
    private int position;
    private Token currentToken;
    
    public SimpleLexer(String input) {
        this.input = input.trim();
        this.position = 0;
    }
    
    @Override
    public Token nextToken() {
        skipWhitespace();
        
        if (position >= input.length()) {
            return new Token(TokenType.EOF, "");
        }
        
        char current = input.charAt(position);
        
        // 处理标识符和关键字
        if (Character.isLetter(current)) {
            return handleIdentifierOrKeyword();
        }
        
        // 处理数字
        if (Character.isDigit(current)) {
            return handleNumber();
        }
        
        // 处理运算符
        if (isOperator(current)) {
            return handleOperator();
        }
        
        // 处理逗号
        if (current == ',') {
            position++;
            return new Token(TokenType.COMMA, ",");
        }
        
        throw new SQLParserException("Unexpected character: " + current);
    }
    
    @Override
    public Token peek() {
        int savedPosition = position;
        Token savedToken = currentToken;
        Token nextToken = nextToken();
        position = savedPosition;
        currentToken = savedToken;
        return nextToken;
    }
    
    private void skipWhitespace() {
        while (position < input.length() && Character.isWhitespace(input.charAt(position))) {
            position++;
        }
    }
    
    private Token handleIdentifierOrKeyword() {
        StringBuilder sb = new StringBuilder();
        while (position < input.length() && 
               (Character.isLetterOrDigit(input.charAt(position)) || input.charAt(position) == '_')) {
            sb.append(input.charAt(position++));
        }
        
        String value = sb.toString();
        TokenType type;
        
        switch (value.toUpperCase()) {
            case "SELECT": type = TokenType.SELECT; break;
            case "DELETE": type = TokenType.DELETE; break;
            case "FROM": type = TokenType.FROM; break;
            case "WHERE": type = TokenType.WHERE; break;
            default: type = TokenType.IDENTIFIER;
        }
        
        return new Token(type, value);
    }
    
    private Token handleNumber() {
        StringBuilder sb = new StringBuilder();
        while (position < input.length() && Character.isDigit(input.charAt(position))) {
            sb.append(input.charAt(position++));
        }
        return new Token(TokenType.NUMBER, sb.toString());
    }
    
    private Token handleOperator() {
        char current = input.charAt(position++);
        StringBuilder operator = new StringBuilder().append(current);
        
        // 处理双字符运算符
        if (position < input.length()) {
            char next = input.charAt(position);
            if ((current == '>' && next == '=') ||
                (current == '<' && (next == '=' || next == '>'))) {
                operator.append(next);
                position++;
            }
        }
        
        return new Token(TokenType.OPERATOR, operator.toString());
    }
    
    private boolean isOperator(char c) {
        return c == '=' || c == '>' || c == '<';
    }
} 